#!/bin/bash
#chkconfig: 345 20 80
# description: jmsservice daemon
# processname: jmsservice
# jmsservice daemon

QUEUE_MANAGER=OM_QMGR
PORT=1414
MQ_BIN=/opt/mqm/bin
MQ_USER=mqm

function stopJMSServer {
	su - ${MQ_USER} -c "$MQ_BIN/endmqm $QUEUE_MANAGER"
	sleep 3
	current_process=$(ps -ef |grep $MQ_BIN |grep -v grep | awk '{print $2}' | head -1)
	while [ "$current_process" != "" ]
	do
		su - ${MQ_USER} -c "kill -9 $current_process" 
		current_process=$(ps -ef |grep $MQ_BIN |grep -v grep | awk '{print $2}' | head -1)
	done
	echo "Queue manager $QUEUE_MANAGER stopped"
}

function startJMSServer {
	su - ${MQ_USER} -c "$MQ_BIN/strmqm $QUEUE_MANAGER"
	su - ${MQ_USER} -c "nohup $MQ_BIN/runmqlsr -m $QUEUE_MANAGER -t TCP -p $PORT >/dev/null 2>&1 &"
	echo "Queue manager $QUEUE_MANAGER started"
}

function usage {
	echo "Usage $0 {start|stop|restart}"
}

case "$1" in 
	start) 
		startJMSServer 
	;;
	status) 
		statusJMSServer
	;;
	stop)
		stopJMSServer
	;;
	restart)
		stopJMSServer
		startJMSServer
	;;
	*)
		usage
	;;
esac